/**
 ******************************************************************************
 * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
 * @author  MCD Application Team
 * @version V3.5.0
 * @date    08-April-2011
 * @brief   Main Interrupt Service Routines.
 *          This file provides template for all exceptions handler and
 *          peripherals interrupt service routine.
 ******************************************************************************
 * @attention
 *
 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 *
 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "PID.h"
// #include "TIMRegister.h"
#include "IRCtrol.h"
#include "interface.h"
#include "motor.h"
#include <stdint.h>

// #include <time.h>
// #include "redvoid.h"

/** @addtogroup STM32F10x_StdPeriph_Template
 * @{
 */

extern PID DirCtrl, SpeedCtrl;
// extern char redrayStatus;
extern uint32_t powerTime;
extern unsigned char irDetected;
// extern unsigned char ir_rec_flag; // 遥控接收数据标志位 1 有新数据 0 没有

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
 * @brief  This function handles NMI exception.
 * @param  None
 * @retval None
 */
void NMI_Handler(void) {}

/**
 * @brief  This function handles Hard Fault exception.
 * @param  None
 * @retval None
 */
void HardFault_Handler(void) {
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1) {
  }
}

/**
 * @brief  This function handles Memory Manage exception.
 * @param  None
 * @retval None
 */
void MemManage_Handler(void) {
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1) {
  }
}

/**
 * @brief  This function handles Bus Fault exception.
 * @param  None
 * @retval None
 */
void BusFault_Handler(void) {
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1) {
  }
}

/**
 * @brief  This function handles Usage Fault exception.
 * @param  None
 * @retval None
 */
void UsageFault_Handler(void) {
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1) {
  }
}

/**
 * @brief  This function handles SVCall exception.
 * @param  None
 * @retval None
 */
void SVC_Handler(void) {}

/**
 * @brief  This function handles Debug Monitor exception.
 * @param  None
 * @retval None
 */
void DebugMon_Handler(void) {}

/**
 * @brief  This function handles PendSVC exception.
 * @param  None
 * @retval None
 */
void PendSV_Handler(void) {}

/**
 * @brief  This function handles SysTick Handler.
 * @param  None
 * @retval None
 */
void SysTick_Handler(void) {}

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

void EXTI15_10_IRQHandler() {
  if (EXTI_GetITStatus(IRIN_EXITLINE) != RESET) // 红外中断
  {
    EXTI_ClearITPendingBit(IRIN_EXITLINE); // 清除中断标志

    // IRIntIsr();
    // ir_rec_flag = 1;

    irDetected = 1;
  }
  //	if(EXTI_GetITStatus(IRIN_EXITLINE) != RESET)	//超声波中断
  //	{
  //		EXTI_ClearITPendingBit(IRIN_EXITLINE);//清除中断标志
  //	}
}
#ifdef TEST_MOTOR
static uint32_t tick_20us = 0, tick_10ms = 0;
#endif
#ifdef TEST_PID
static uint32_t tick_20us = 0, tick_10ms = 0;
#endif
static int tick_20us = 0;
extern char TIM_Enabled;
/**
 * @brief  This function handles TIM2 interrupt request.
 * @param  None
 * @retval None
 */
void TIM2_IRQHandler(void) {
  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    if (TIM_Enabled) {

#ifndef CUSTOM_PWM_ENABLED
      tick_1ms++;
      if (tick_1ms >= 10) {
        tick_1ms = 0;
        speed_count++;
        tick_5ms++;
        if (speed_count >= 50) {
          speed_count = 0;
        }
        CarMove();
      }
#elif defined TEST_PID
      tick_20us++;
      if (tick_20us == 500) {
        tick_20us = 0;
        tick_10ms++;
        if (tick_10ms < 100) {
          PID_SetMeasured(&DirCtrl, tick_10ms / 10);
        } else if (tick_10ms < 200) {
          PID_SetMeasured(&DirCtrl, ((int32_t)tick_10ms / 10) - 20);
        } else {
          tick_10ms = 0;
        }
        PID_SetMeasured(&DirCtrl, -10);
      }
#elif defined TEST_MOTOR
      tick_20us++;
      if (tick_20us == 500) {
        tick_20us = 0;
        tick_10ms++;
        if (tick_10ms < 100) {
          setDuty(tick_10ms, tick_10ms);
        } else if (tick_10ms < 200) {
          setDuty(200 - tick_10ms, 200 - tick_10ms);

        } else {
          tick_10ms = 0;
        }
      }
#endif
      PID_Step(&DirCtrl);
      PID_Step(&SpeedCtrl);
      PID2Motor();
      CarMove();
      tick_20us++;
      if (tick_20us == 50) {
        tick_20us = 0;
        powerTime++;
      }
      void IRTimeHandler();
#ifdef DOWNCOUNTER_MODULE_ENABLED
      DownCounter_Tick();
#endif
    }
  }
}

/**
 * @brief  This function handles PPP interrupt request.
 * @param  None
 * @retval None
 */
void PPP_IRQHandler(void) {}

/**
 * @}
 */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF
 * FILE****/
